package cn.bonoon.kernel.io;

import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

public abstract class AbstractFileManager implements FileManager{

	@Override
	public FileInfo save(String dir, MultipartFile file) throws IllegalStateException, IOException{
		return __save(dir, DirectoryStrategy.NONE, FilenameStrategy.NONE, file);
	}

	@Override
	public FileInfo save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartFile file) throws IllegalStateException, IOException{
		return __save(dir, directoryStrategy, filenameStrategy, file);
	}
	
	@Override
	public FileInfo[] save(String dir, MultipartFile... files) throws IllegalStateException, IOException{
		return __save(dir, DirectoryStrategy.NONE, FilenameStrategy.NONE, files);
	}

	@Override
	public FileInfo[] save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartFile... files) throws IllegalStateException, IOException{
		return __save(dir, directoryStrategy, filenameStrategy, files);
	}
	
	@Override
	public FileInfo save(String dir, MultipartHttpServletRequest multipartRequest, String filename) throws IllegalStateException, IOException{
		MultipartFile file = multipartRequest.getFile(filename);
		if(null == file || file.isEmpty()) return null;
		return __save(dir, DirectoryStrategy.NONE, FilenameStrategy.NONE, file);
	}

	@Override
	public FileInfo save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartHttpServletRequest multipartRequest, String filename) throws IllegalStateException, IOException{
		MultipartFile file = multipartRequest.getFile(filename);
		if(null == file || file.isEmpty()) return null;
		return __save(dir, directoryStrategy, filenameStrategy, file);
	}
	
	@Override
	public FileInfo[] save(String dir, MultipartHttpServletRequest multipartRequest, String... filenames) throws IllegalStateException, IOException{
		return __save(dir, DirectoryStrategy.NONE, FilenameStrategy.NONE, multipartRequest, filenames);
	}

	@Override
	public FileInfo[] save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartHttpServletRequest multipartRequest, String... filenames) throws IllegalStateException, IOException{
		return __save(dir, directoryStrategy, filenameStrategy, multipartRequest, filenames);
	}
	
	@Override
	public boolean delete(String path) throws Exception {
		return false;
	}
	
	protected abstract FileInfo __save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartFile file) throws IllegalStateException, IOException;
	protected abstract FileInfo[] __save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartFile... files)throws IllegalStateException, IOException;
	protected abstract FileInfo[] __save(String dir, DirectoryStrategy directoryStrategy, FilenameStrategy filenameStrategy, MultipartHttpServletRequest multipartRequest, String... filenames) throws IllegalStateException, IOException;
	
}
